package org.subside.thread;

import org.springframework.jdbc.core.JdbcTemplate;
import org.subside.Utils.ChineseUtil;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : zhangbo
 * @data : 2024/5/8
 * @description :生成数据
 */
public class create {
    @Resource
    private JdbcTemplate jdbcTemplate;
    public static void main(String[] args) throws Exception {
        List<String>list =new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String insertSql = insert();
            list.add(insertSql);
        }
        //1.导入mysql的jar包
        // 2.注册mysql驱动  反射机制
        Class.forName("com.mysql.jdbc.Driver");
        //3.创建数据库连接   http协议  tcp协议  jdbc协议
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/subside", "root", "1234");
        //4.获取执行者对象
        Statement statement = connection.createStatement();
        //5.执行sql语句
        for (String sql : list) {
            statement.execute(sql);
        }
        //7.关闭连接对象
        connection.close();

    }

    //静态方法，只能调用静态方法
    public static String  insert() throws ParseException {
        //随机生成insert语句参数
        //1.生成姓名
        String name = ChineseUtil.randomChineseName();
        //2.生成薪水
        int salary = ChineseUtil.randomNumber(10000, 50000);
        //3.生成奖金
        int bonus = ChineseUtil.randomNumber(200, 4000);
        //4.生成入职时间
        String time = ChineseUtil.getTime();
        //5.生成部门名称
        String department = ChineseUtil.randomCreateDepartment();
        return createSql(EmpDto.builder().name(name).salary(salary).bonus(bonus).time(time).department(department).build());
    }
    public static String createSql(EmpDto dto) {
        //动态生成insert语句
        StringBuffer sql = new StringBuffer();
        sql.append("insert into emp ( name,salary, bonus, time, department )")
                .append("values ( ")
                //字符串做insert需要加单引号    ' name','department'
                .append(" '")
                .append(dto.getName()).append(" '").append(" ").append(",")
                .append(dto.getSalary()).append(" ").append(",")
                .append(dto.getBonus()).append(" ").append(",").append(" '")
                .append(dto.getTime()).append(" '").append(" ").append(",").append("'")
                .append(dto.getDepartment()).append(" '").append(" ").append(")");
        return sql.toString();
    }
    public void setJdbcTemplate() throws Exception {
        //1.导入mysql的jar包
        // 2.注册mysql驱动  反射机制
        Class.forName("com.mysql.jdbc.Driver");
        //3.创建数据库连接   http协议  tcp协议  jdbc协议
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/subside", "root", "1234");
        //4.获取执行者对象
        Statement statement = connection.createStatement();
        //5.执行sql语句
        ResultSet resultSet = statement.executeQuery("select * from goods ");
        //6.执行结果，自行处理
        while (resultSet.next()) {
            System.out.println(resultSet.getInt("id") + resultSet.getString("name") + resultSet.getString("parent_id"));
        }
        //7.关闭连接对象
        connection.close();
    }
}
